﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Resources;
using System.Reflection;
using System.Collections.Generic;

namespace ArgusControls
{
	public class EnumComboBox : ComboBox
	{
		Type type;

		public object SelectedEnum { get; protected set; }

		private Dictionary<string, EnumComboBoxItem> dictCBI;

		public EnumComboBox()
		{
			this.SelectionChanged += EnumComboBox_SelectionChanged;
		}

		public void SetEnumType(Type enumType)
		{
			this.Items.Clear();
			this.dictCBI = new Dictionary<string, EnumComboBoxItem>();
			this.type = enumType;
			string[] names = Enum.GetNames(this.type);
			foreach (string name in names)
			{
				EnumComboBoxItem cbi = new EnumComboBoxItem(name);
				cbi.Content = name;
				this.dictCBI.Add(name, cbi);
				
				this.Items.Add(cbi);
			}
			
			this.SelectedIndex = 0;
		}

		void EnumComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			EnumComboBoxItem cbi = (EnumComboBoxItem)this.SelectedItem;
			this.SelectedEnum = Enum.Parse(this.type, cbi.EnumName, true);
		}

		public bool Select(object value)
		{
			if (value.GetType() != this.type)
				return false;

			EnumComboBoxItem cbi;
			if (this.dictCBI.TryGetValue(value.ToString(), out cbi) == false)
				return false;
			this.SelectedItem = cbi;
			return true;
		}
	}
}
